package tech.aistar.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import tech.aistar.dto.Comples;
import tech.aistar.entity.Collect;
import tech.aistar.entity.Completion;
import tech.aistar.entity.User;
import tech.aistar.service.ICollectService;
import tech.aistar.service.impl.CompletionServiceImpl;
import tech.aistar.service.impl.UrankServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller

public class IndexAction {
    @Autowired
    private CompletionServiceImpl completion;
    @Autowired
    private UrankServiceImpl urankService;
    @Autowired
    private ICollectService collectService;
    /**
     *
        加载首页题库信息
     */
    @RequestMapping("/index")
    public String load(Model model, HttpServletRequest req, HttpSession session){
        List<Completion> completions = completion.findAll();
        List<Comples> comples  = new ArrayList<>();
        String uid  = req.getParameter("uid");
       // System.out.println(uid);


        User user = (User) session.getAttribute("user");
       // System.out.println(user);
        if (user!=null){
            List<Collect> collects = collectService.findAllUserCol(String.valueOf(user.getUsId()));
            for (Completion c : completions) {
                Comples comple = new Comples();
                comple.setComStem(c.getComStem());
                comple.setComDiff(c.getComDiff());
                comple.setComLanguage(c.getComLanguage());
                for (Collect c1 : collects) {
                   // System.out.println(c.getComStem());
                   // System.out.println(c1.getColStem());
                    if (c.getComStem().equals(c1.getColStem())){
                        comple.setIscollect(true);
                        break;
                    }
                }
                comples.add(comple);
            }

        }else{
            for (Completion c : completions) {
                Comples comple = new Comples();
                comple.setComStem(c.getComStem());
                comple.setComDiff(c.getComDiff());
                comple.setComLanguage(c.getComLanguage());
                comple.setIscollect(false);
                comples.add(comple);
            }
        }
        model.addAttribute("completions",comples);
        model.addAttribute("javarank",urankService.findScore());
        model.addAttribute("pythonrank",urankService.findScore1());
        model.addAttribute("crank",urankService.findScore2());
        model.addAttribute("webrank",urankService.findScore3());
        model.addAttribute("sqlrank",urankService.findScore4());
        return  "index";
    }
}
